<%@page import="com.alibaba.fastjson.JSONArray"%>
<%@page import="java.util.Map.Entry"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.LinkedHashMap"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Map"%>
<%@page import="com.alibaba.fastjson.JSONObject"%>
<%@page import="weaver.conn.RecordSet"%>
<%@page import="weaver.general.Util"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
	String filterValue = Util.null2String(request.getParameter("filterValue")).trim();
	
	String sqlWhere = "";
	if( !"".equals(filterValue) ){
		int id = Util.getIntValue(filterValue,0);
		sqlWhere += " AND ( A.id = "+id+" or A.workflowname like '%"+filterValue+"%' )";
	}

	String sql = "SELECT A.*,B.typename FROM workflow_base A INNER JOIN workflow_type B ON A.workflowtype = B.id WHERE 1=1 "+ sqlWhere +" ORDER BY B.dsporder,A.id";
	JSONArray arr = new JSONArray();
	Map<String,List<JSONObject>> map = new LinkedHashMap<String,List<JSONObject>>();
	RecordSet rs = new RecordSet();
	rs.writeLog(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+sql);
	if(rs.executeSql(sql)){
		while(rs.next()){
			int workflowtype = Util.getIntValue(rs.getString("workflowtype"),-1);
			String workflowtypeName = Util.null2String(rs.getString("typename"));
			String key = workflowtype+"@"+workflowtypeName;
			if(!map.containsKey(key)){
				map.put(key,new ArrayList<JSONObject>());
			}
			JSONObject json = new JSONObject();
			String id = Util.null2String(rs.getString("id"));
			String name = Util.null2String(rs.getString("workflowname"));
			String version = Util.null2String(rs.getString("version"));
			json.put("id",id);
			json.put("title",name);
			json.put("ref","li_"+id);
			json.put("version","".equals(version)?"1":version);
			map.get(key).add(json);
		}
		
		for(Entry<String,List<JSONObject>> entry: map.entrySet()){
			String[] strs = Util.TokenizerString2(entry.getKey(),"@");
			JSONObject json = new JSONObject();
			json.put("title",strs[1]);
			json.put("children",entry.getValue());
			arr.add(json);
		}
	}
	
	out.print(arr.toJSONString());
	
%>
